/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.maps;

import java.util.LinkedList;
import rpg.gfx.Bitmap;

/**
 *
 * @author rothens
 */
/**
 * This is a class that contains all tile layers.
 */
public class MapTile {

    public LinkedList<AutoTileData> tilesOver = new LinkedList<AutoTileData>();
    private Tiles.Tile landscape = null;
    private Tiles.RoadTile road = null;
    private Tiles.Tile layer1 = null;
    private Tiles.Tile layer2 = null;
    private Bitmap overlay = null;
    private int mapcolor = 0;
    private int bigMapColor[] = new int[4];
    private int veryBigMapColor[] = new int[64];
    public MapTile() {
    }

    public MapTile(Tiles.Tile landscape) {
        this.landscape = landscape;
    }

    public void setRoad(Tiles.RoadTile road) {
        this.road = road;
    }

    public Tiles.RoadTile getRoad() {
        return road;
    }

    public void setLandcape(Tiles.Tile landscape) {
        this.landscape = landscape;
    }

    public void setLayer1(Tiles.Tile layer1) {
        this.layer1 = layer1;
    }

    public void setLayer2(Tiles.Tile layer2) {
        this.layer2 = layer2;
    }

    public Tiles.Tile getLandscape() {
        return landscape;
    }

    public Tiles.Tile getLayer1() {
        return layer1;
    }

    public Tiles.Tile getLayer2() {
        return layer2;
    }

    public void generateOverImage() {
        overlay = new Bitmap(64, 64);
        overlay.draw(landscape.bitmap, 0, 0);
        for (AutoTileData atd : tilesOver) {
            Tiles.AutoTile at = MapHandler.tileset.autoTiles.get(atd.tile);
            if (at == null) {
                continue;
            }
            overlay.drawOn(at.getAuto((atd.corner + 16)), 0, 0);
            overlay.drawOn(at.getAuto(atd.side), 0, 0);
        }
        if (layer1 != null) {
            overlay.drawOn(layer1.bitmap, 0, 0);
        }
        if (road != null) {
            overlay.drawOn(road.bitmaps[road.getState()], 0, 0);
        }
        getMapColors();

    }

    public void getMapColors() {
        mapcolor = overlay.getAverageColor();
        Bitmap mc = new Bitmap(32, 32);
        for (int y = 0; y < 2; y++) {
            for (int x = 0; x < 2; x++) {
                mc.draw(overlay, x*-32, y*-32);
                bigMapColor[x+y*2] = mc.getAverageColor();
            }
        }
        Bitmap bmc = new Bitmap(8,8);
        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                bmc.draw(overlay, x*-8, y*-8);
                veryBigMapColor[x+y*8] = bmc.getAverageColor();
            }
        }
    }

    public int getBigMapColor(int i) {
        return bigMapColor[i];
    }
    
    public int getVeryBigMapColor(int i) {
        return veryBigMapColor[i];
    }

    public Bitmap getOverImage() {
        return overlay;
    }

    public boolean isPassable() {

        return (road != null)
                || ((landscape == null || landscape.passable)
                && (layer1 == null || layer1.passable)
                && (layer2 == null || layer2.passable));
    }

    public void addAutoData(char c, int sides, int corners) {
        tilesOver.add(new AutoTileData(sides, corners, c));
    }

    public void addAutoData(AutoTileData d) {
        tilesOver.add(d);
    }

    /**
     * Checks if light can pass through this tile.
     *
     * @return
     */
    public boolean isTransparent() {
        return (landscape != null && landscape.transparent) && (layer1 == null || layer1.transparent) && (layer2 == null || layer2.transparent);
    }

    public int getMapcolor() {
        return mapcolor;
    }
}